package com.oauth.application.server.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Required;

import com.oauth.application.client.beans.ApplicationClientBean;
import com.oauth.application.client.rpc.ApplicationRemoteService;
import com.oauth.common.server.OAuthGwtController;
import com.oauth.service.application.beans.ApplicationServiceBean;
import com.oauth.service.application.beans.MemberApplicationRelation;
import com.oauth.service.application.service.ApplicationService;
import com.oauth.service.generic.beans.OAuthServiceBean;

public class ApplicationGwtController extends OAuthGwtController implements
		ApplicationRemoteService {

	private ApplicationService mApplicationService;
	/**
	 * 
	 */
	private static final long serialVersionUID = -2118333321471056975L;

	public ArrayList<ApplicationClientBean> getMyApplications() {
		Long memberId = getMemberId();

		List<ApplicationServiceBean> memberApplications = getApplicationService()
				.getMemberApplications(memberId);

		ArrayList<ApplicationClientBean> apps = new ArrayList<ApplicationClientBean>();
		for (ApplicationServiceBean serviceBean : memberApplications) {
			ApplicationClientBean application = new ApplicationClientBean(serviceBean.getName(),
					serviceBean.getKey(), serviceBean.getAddUrl(), serviceBean.getIconImageUrl(),
					serviceBean.getAppImageUrl());
			apps.add(application);
		}

		return apps;

	}

	public ArrayList<ApplicationClientBean> getRecommendedApplications() {
		ArrayList<ApplicationClientBean> apps = new ArrayList<ApplicationClientBean>();
		/*
		 * apps.add(new ApplicationClientBean("Yahoo", 1l, "/Yahoo.html"));
		 * apps.add(new ApplicationClientBean("Google", 2l, "/Google.html"));
		 * apps.add(new ApplicationClientBean("Yahoo", 1l, "/Yahoo.html"));
		 */return apps;
	}

	public String subscribeToApplication(Long pApplicationId) {
		Long memberId = getMemberId();

		MemberApplicationRelation addMemberToApplication = getApplicationService()
				.addMemberToApplication(memberId, pApplicationId);
		if (addMemberToApplication != null && addMemberToApplication.getApplicationIdSet() != null) {
			if (addMemberToApplication.getApplicationIdSet().contains(pApplicationId)) {
				return "success";
			}
		}
		return "Failed";
	}

	public String unSubscribeFromApplication(Long pApplicationId) {
		Long memberId = getMemberId();

		List<ApplicationServiceBean> memberApplications = getApplicationService()
				.getMemberApplications(memberId);

		for (ApplicationServiceBean applicationServiceBean : memberApplications) {
			if (applicationServiceBean.getKey().equals(pApplicationId)) {
				// Confirmed that Member has subsribed to this application!!
				MemberApplicationRelation appRelation = getApplicationService()
						.removeMemberToApplication(memberId, pApplicationId);
				if (!appRelation.getApplicationIdSet().contains(pApplicationId)) {
					// Success
					return "success";
				} else {
					return "Failed";
				}
			}
		}
		return "Failed";
	}

	public ArrayList<ApplicationClientBean> getUnSubscribedApplications() {
		Long memberId = getMemberId();

		List<ApplicationServiceBean> memberApplications = getApplicationService()
				.getUnSubscribedApplications(memberId);

		ArrayList<ApplicationClientBean> apps = new ArrayList<ApplicationClientBean>();
		for (ApplicationServiceBean serviceBean : memberApplications) {
			ApplicationClientBean application = new ApplicationClientBean(serviceBean.getName(),
					serviceBean.getKey(), serviceBean.getAddUrl(), serviceBean.getIconImageUrl(),
					serviceBean.getAppImageUrl());

			apps.add(application);
		}

		return apps;
	}

	public ApplicationService getApplicationService() {
		return mApplicationService;
	}

	@Required
	public void setApplicationService(ApplicationService pApplicationService) {
		mApplicationService = pApplicationService;
	}

	@Override
	protected <T extends OAuthServiceBean> Class<T> getServiceBeanClass() {
		// TODO Auto-generated method stub
		return null;
	}

	// protected OAuthAccessTokenBean getAccessTokenFromProvider() {
	// // TODO Auto-generated method stub
	// return null;
	// }

}
